package com.example.wisdomclassroomapi.domain.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.wisdomclassroomapi.domain.entity.FilePeriodEntity;
import com.example.wisdomclassroomapi.domain.repository.FilePeriodRepository;
import com.example.wisdomclassroomapi.infrastructure.mapper.FilePeriodMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Package: com.example.wisdomclassroomapi.domain.service
 * @Description: <>
 * @Author: scholes
 * @CreateDate: 2022-04-20
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-20
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Service
public class FilePeriodDomainService {
    @Resource
    private FilePeriodRepository filePeriodRepository;
    @Resource
    private FilePeriodMapper filePeriodMapper;

    public void add(FilePeriodEntity entity) {
        filePeriodRepository.add(entity);
    }

    public FilePeriodEntity findById(String id) {
        return filePeriodRepository.findById(id);
    }

    public int updateById(FilePeriodEntity entity) {
        return filePeriodRepository.updateById(entity);
    }

    public int deleteById(String id) {
        return filePeriodRepository.deleteById(id);
    }

    public IPage<FilePeriodEntity> page(FilePeriodEntity entity, IPage page) {
        return filePeriodRepository.page(entity, page);
    }

    public List<FilePeriodEntity> list(FilePeriodEntity entity) {
        return filePeriodRepository.list(entity);
    }


    //保存文件
    public void save(FilePeriodEntity file1) {
        //根据 数据库的 文件标识来查询 当前视频 是否存在
        LambdaQueryWrapper<FilePeriodEntity> lambda = new QueryWrapper<FilePeriodEntity>().lambda();
        lambda.eq(FilePeriodEntity::getFileKey, file1.getFileKey());
        List<FilePeriodEntity> fileDTOS = filePeriodMapper.selectList(lambda);
        //如果存在就话就修改
        if (fileDTOS.size() != 0) {
            //根据key来修改
            LambdaQueryWrapper<FilePeriodEntity> lambda1 = new QueryWrapper<FilePeriodEntity>().lambda();
            lambda1.eq(FilePeriodEntity::getFileKey, file1.getFileKey());
                filePeriodMapper.update(file1, lambda1);
        } else {
            //不存在就添加
                filePeriodMapper.insert(file1);
        }
    }

    //检查文件
    public List<FilePeriodEntity> check(String key) {
        LambdaQueryWrapper<FilePeriodEntity> lambda = new QueryWrapper<FilePeriodEntity>().lambda();
        lambda.eq(FilePeriodEntity::getFileKey, key);
        List<FilePeriodEntity> dtos = filePeriodMapper.selectList(lambda);
        return dtos;
    }
}